package com.lenovo.lcdm.team.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lenovo.lcdm.common.enums.ResponseMsgEnum;
import com.lenovo.lcdm.common.model.ResponseVo;
import com.lenovo.lcdm.team.common.util.ContextUtil;
import com.lenovo.lcdm.team.entity.TeamRole;
import com.lenovo.lcdm.team.service.ITeamRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/team")
public class RoleController {

    @Autowired
    private ITeamRoleService roleService;

    @GetMapping("/role/list")
    public ResponseVo<Page<TeamRole>> roleList(@RequestParam int pageNo, @RequestParam int pageSize, @RequestParam(required = false) String roleName) {
        Page<TeamRole> page = new Page<>(pageNo, pageSize);
        LambdaQueryWrapper<TeamRole> qw = new LambdaQueryWrapper<>();
        qw.like(StringUtils.isNotBlank(roleName), TeamRole::getRoleName, roleName);
        qw.orderByAsc(TeamRole::getRoleName);
        List<TeamRole> data = roleService.list(page, qw);
        page.setRecords(data);
        ResponseVo<Page<TeamRole>> responseVo = new ResponseVo<>();
        responseVo.setData(page);
        return responseVo;
    }

    @GetMapping("/role/all")
    public ResponseVo<List<TeamRole>> getAllRole() {
        ResponseVo<List<TeamRole>> responseVo = new ResponseVo<>();
        List<TeamRole> data = roleService.list();
        responseVo.setData(data);
        return responseVo;
    }

    @PostMapping("/role")
    public ResponseVo saveRole(@RequestBody TeamRole role) {
        ResponseVo responseVo = new ResponseVo<>();
        try {
            if(role.getId() == null){
                role.setCreator(ContextUtil.getCurrentUser());
            }
            role.setModifier(ContextUtil.getCurrentUser());
            roleService.saveRole(role);
        }catch (Exception e){
            responseVo.setCode(ResponseMsgEnum.FAIL.getCode());
            responseVo.setMessage(e.getMessage());
        }
        return responseVo;
    }

    @DeleteMapping("/role/{id}")
    public ResponseVo deleteRole(@PathVariable("id") Long id) {
        ResponseVo responseVo = new ResponseVo<>();
        try {
            roleService.deleteRole(id);
        }catch (Exception e){
            responseVo.setCode(ResponseMsgEnum.FAIL.getCode());
            responseVo.setMessage(e.getMessage());
        }
        return responseVo;
    }

    @GetMapping("/role/{id}")
    public ResponseVo<TeamRole> getRole(@PathVariable("id") Long id) {
        ResponseVo<TeamRole> responseVo = new ResponseVo<>();
        try {
            TeamRole role = roleService.getById(id);
            responseVo.setData(role);
        }catch (Exception e){
            responseVo.setCode(ResponseMsgEnum.FAIL.getCode());
            responseVo.setMessage(e.getMessage());
        }
        return responseVo;
    }
}
